#include <QtGui>
#include <QToolBar>
#include <QGridLayout>
#include <QVector>
#include <QWidget>
#include <QToolButton>
#include <QSizePolicy>

#include <utility> // std::pair

#include "TicTacToe.h"
#include "case.h"

// First is x and second is y
typedef std::pair<int, int> coordonnees;

TicTacToe::TicTacToe::TicTacToe() {
  m_Vector = QVector<QPushButton*>();
  //m_Vector->reserve(9);
  m_Playeur = true;

  QGridLayout *ticTacToeLayout = new QGridLayout();
  for(int i = 0; i < 9; ++i) {
    coordonnees c = index2coordonnees(i);
    Case *caseButton = new Case("Case");
    caseButton->setFixedSize(50, 50);

    QObject::connect(caseButton, SIGNAL(clickedWithId(Case *)), this, SLOT(caseClicked(Case *c)));

    ticTacToeLayout->addWidget(caseButton, c.first, c.second);
    m_Vector.push_back(caseButton);
  }
  QWidget *centralWidget = new QWidget();
  centralWidget->setLayout(ticTacToeLayout);
  setCentralWidget(centralWidget);

  addToolBar(getToolBar());
}

QToolBar* TicTacToe::TicTacToe::getToolBar() const{
  QToolBar *toolBar = new QToolBar();
  toolBar->addWidget(new QPushButton("&New"));
  toolBar->addWidget(new QPushButton("&Compute"));

  QPushButton *exitButton = new QPushButton("&Exit");
  QObject::connect(exitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
  toolBar->addWidget(exitButton);
  return toolBar;
}

void TicTacToe::TicTacToe::click() {
  m_Playeur = !m_Playeur;
}

coordonnees TicTacToe::TicTacToe::index2coordonnees(const int index) {
  return coordonnees(index % 3, index / 3);
}

int TicTacToe::TicTacToe::coord2index(const coordonnees c) {
  return 3*c.first+c.second;
}

void TicTacToe::TicTacToe::caseClicked(Case *c) {
  c->setText("clické !");
}
